package Iol;

import java.io.*;
import java.util.stream.Collectors;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.19.17.17
 */
public class Io4 {
    public static void main(String[] args) {
     String dir="D:\\JavaStudy";
     var e=new Io4();
     e.count(new File(dir));
    }

    public void append(String source,File dst){
        try (var out=new BufferedWriter(new FileWriter(dst,true))) {
              out.write(source);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void append(File src,File dst){
        try(var read=new BufferedReader(new FileReader(src)) ) {
            StringBuilder sbu=new StringBuilder();
            sbu.append("------------------------------------------------------------");
            sbu.append(String.format("--文件名称：%s%n",src.getName()));
            sbu.append(String.format("--文件路径：%s%n",src.getAbsolutePath()));
            String info=read.lines().collect(Collectors.joining("\n"));
            //out.write(read.lines().collect(Collectors.joining("\n")));
            sbu.append(info+"\n\n");
            append(sbu.toString(),dst);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void count(File dir){
        for(File t:dir.listFiles()){
            if(t.isDirectory()){
                count(t);
            }else if(t.getName().endsWith(".java")){
                append(t,new File("src.txt"));
                //System.out.println(t.getName());
            }
        }
        if(dir.isFile()&&dir.getName().endsWith(".java")){
            System.out.println(dir);
        }
    }

}
